<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <script src="../js/jquery-3.7.1.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f9ff;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-image:
                    linear-gradient(rgba(255, 255, 255, 0.8), rgba(255, 255, 255, 0.8)),
                    url('https://img.freepik.com/free-vector/hand-drawn-school-background_23-2149464866.jpg');
            background-size: cover;
            background-position: center;
        }

        .login-container {
            background-color: rgba(255, 255, 255, 0.95);
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 5px 25px rgba(100, 149, 237, 0.2);
            width: 380px;
            border: 1px solid rgba(100, 149, 237, 0.3);
            position: relative;
            overflow: hidden;
        }

        .login-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 5px;
            height: 100%;
            background: linear-gradient(to bottom, #6495ed, #87cefa);
        }

        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #4169e1;
            font-size: 28px;
            font-weight: 600;
            position: relative;
        }

        h2::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 50px;
            height: 3px;
            background: linear-gradient(to right, #6495ed, #87cefa);
            border-radius: 3px;
        }

        .form-group {
            margin-bottom: 25px;
            position: relative;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
            font-weight: 500;
        }

        input[type="email"],
        input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            background-color: rgba(240, 248, 255, 0.7);
            border: 1px solid #d3e0f9;
            border-radius: 8px;
            font-size: 16px;
            box-sizing: border-box;
            color: #333;
            transition: all 0.3s;
        }

        input:focus {
            border-color: #6495ed;
            box-shadow: 0 0 0 3px rgba(100, 149, 237, 0.2);
            outline: none;
            background-color: #fff;
        }

        .form-options {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
            font-size: 14px;
        }

        .remember-me {
            display: flex;
            align-items: center;
            color: #666;
        }

        .remember-me input {
            margin-right: 8px;
            accent-color: #6495ed;
        }

        .forgot-password {
            color: #6495ed;
            text-decoration: none;
            transition: color 0.3s;
        }

        .forgot-password:hover {
            color: #4169e1;
            text-decoration: underline;
        }

        .submit-btn {
            width: 100%;
            padding: 14px;
            background: linear-gradient(to right, #6495ed, #87cefa);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            box-shadow: 0 4px 15px rgba(100, 149, 237, 0.3);
        }

        .submit-btn:hover {
            background: linear-gradient(to right, #5b8ae6, #7ac1f5);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(100, 149, 237, 0.4);
        }

        .submit-btn:disabled {
            background: #ccc;
            color: #999;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .register-link {
            text-align: center;
            margin-top: 25px;
            color: #666;
        }

        .register-link a {
            color: #6495ed;
            text-decoration: none;
            font-weight: 600;
            transition: color 0.3s;
        }

        .register-link a:hover {
            color: #4169e1;
            text-decoration: underline;
        }

        .error-message {
            color: #ff6b6b;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .success-message {
            color: #4caf50;
            font-size: 14px;
            text-align: center;
            margin-bottom: 20px;
            display: none;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>用户登录</h2>
    <div id="successMessage" class="success-message"></div>
    <form id="loginForm">
        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" id="email" name="email" placeholder="请输入注册邮箱" required>
            <span id="emailError" class="error-message"></span>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
            <span id="passwordError" class="error-message"></span>
        </div>

        <div class="form-options">
            <label class="remember-me">
                <input type="checkbox" id="rememberMe"> 记住我
            </label>
            <a href="#" class="forgot-password">忘记密码?</a>
        </div>

        <button type="submit" id="loginBtn" class="submit-btn">登录</button>

        <div class="register-link">
            还没有账号？<a href="register.html">立即注册</a>
        </div>
    </form>
</div>

<script>
    $(document).ready(function() {
        // 检查本地存储中是否有记住的登录信息
        const rememberedEmail = localStorage.getItem('rememberedEmail');
        if (rememberedEmail) {
            $('#email').val(rememberedEmail);
            $('#rememberMe').prop('checked', true);
        }

        // 表单提交处理
        $('#loginForm').submit(function(e) {
            e.preventDefault();

            // 验证表单
            if (!validateForm()) {
                return;
            }

            // 禁用提交按钮
            $('#loginBtn').prop('disabled', true).text('登录中...');

            // 获取表单数据
            const formData = {
                email: $('#email').val(),
                password: $('#password').val()
            };

            // 发送Ajax请求
            $.ajax({
                url: '/NiDer/LoginServlet',
                type: 'POST',
                data: formData,
                dataType: 'json',
                success: function(response) {
                    if (response.success) {
                        // 处理记住我选项
                        if ($('#rememberMe').is(':checked')) {
                            localStorage.setItem('rememberedEmail', formData.email);
                        } else {
                            localStorage.removeItem('rememberedEmail');
                        }

                        // 保存用户信息到本地存储
                        localStorage.setItem('user', JSON.stringify(response.data));

                        // 显示成功消息
                        $('#successMessage').text('登录成功！正在跳转...').show();

                        // 跳转到首页
                        setTimeout(function() {
                            window.location.href = 'index.html';
                        }, 1500);
                    } else {
                        showError('登录失败: ' + response.message);
                        $('#loginBtn').prop('disabled', false).text('登录');
                    }
                },
                error: function(xhr, status, error) {
                    showError('网络错误: ' + error);
                    $('#loginBtn').prop('disabled', false).text('登录');
                }
            });
        });

        // 表单验证
        function validateForm() {
            let isValid = true;

            // 验证邮箱
            const email = $('#email').val();
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!email) {
                $('#emailError').text('请输入邮箱').show();
                isValid = false;
            } else if (!emailRegex.test(email)) {
                $('#emailError').text('请输入有效的邮箱地址').show();
                isValid = false;
            } else {
                $('#emailError').hide();
            }

            // 验证密码
            const password = $('#password').val();
            if (!password) {
                $('#passwordError').text('请输入密码').show();
                isValid = false;
            } else {
                $('#passwordError').hide();
            }

            return isValid;
        }

        // 显示错误消息
        function showError(message) {
            $('#successMessage').hide();
            alert(message); // 简单使用alert显示错误，可以根据需要修改
        }
    });
</script>
</body>
</html>